<?php

class CommentController extends Controller
{
	public function filters()
    {
        return array('accessControl');
    }
	public function accessRules()
    {
        return array(
        		array('allow',
        			'actions'=>array('list'),
        			'users'=>array('*'),
        		),
            array('allow',
                'actions'=>array('add','delete','edit','new','update'),
                'users'=>array('@'),
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }
	protected $_model;
	public function actionAdd()
	{	$id = getQuery('id');
		$book = UrBook::model()->findByPK($id);
		$this->_model = new UrComment;
		$this->render('add',array('bookinfo'=>$book,'model'=>$this->_model));
	}

	public function actionList(){
		$type = getQuery('type');	//type=1 show all comments by bookid
		if($type==1){
			$bid = getQuery('bid');
			$criteria = new CDbCriteria;
			
			$criteria -> condition = 'bid=:bid';
			$criteria -> params = array(':bid'=>$bid);
			$commentCount = UrComment::model()->count($criteria);
			$criteria -> order = 'create_datetime desc';

			$pages = new CPagination($commentCount);
			$pages -> pageSize = 15;
			$pages->applyLimit($criteria);
			
	
			$commentlists = UrComment::model()->findAll($criteria);
			
			$bookinfo = UrBook::model()->findByPK($bid);
			$this->render('list',array('lists'=>$commentlists,'bookinfo'=>$bookinfo,'pages'=>$pages));	
			}
		if($type===null){
			$criteria = new CDbCriteria;
			$commentCount = UrComment::model()->count($criteria);
			$criteria -> order = 'create_datetime desc';
			$pages = new CPagination($commentCount);
			$pages -> pageSize = 15;
			$pages->applyLimit($criteria);
			$commentlists = UrComment::model()->findAll($criteria);
			$this->render('index',array('lists'=>$commentlists,'pages'=>$pages));			
		}
		
	}
	public function actionDelete()
	{
		$this->render('delete');
	}

	public function actionEdit()
	{
		$this->render('edit');
	}

	public function actionNew()
	{
		$subject = getPost('subject');
		$content = getPost('content');
		$bid = getPost('bid');
		$comment = new UrComment;
		$comment->parentId = 0;
		$comment->uid = user()->id;
		$comment->username = user()->name;
		$comment->bid = $bid;
		$comment->subject = $subject;
		$comment->create_datetime = $comment->update_datetime = time();

		if($comment->save()){
		$commentEntry = new UrCommentEntry;
		$commentEntry->cid = $comment->id;
		$commentEntry->content = $content;
		
		if($commentEntry->save()){
			$this->redirect($this->createUrl('book/show',array('id'=>$bid)));
			}
		}
		
	}

	public function actionUpdate()
	{
		$this->render('update');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
				var_dump($pages);	);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}
